﻿using System;
using Microsoft.Win32;
using System.Xml;
using System.Threading;

namespace SMS_Agent
{
    public class AppSetting
    {
        static private AppSetting aps;
        public static string configFileName;
        static private XmlDocument xml = new XmlDocument();
        static private XmlNode appSettingsElement;
        static private XmlElement newElement;
        static string m_AppName = "";

        private System.Globalization.CultureInfo CultureInfo = new System.Globalization.CultureInfo("vi-VN");

        private string _RegistryKey = @"Software\Victory\eTicket";

        public string c_RegistryKey
        {
            get { return _RegistryKey; }
            set { _RegistryKey = value; }
        }
        // set property cho _RegistryKey truoc khi goi 02 ham duoi day
        public string getRegValue(string aKey)
        {
            string avalue = null;
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey(_RegistryKey);
            if (regKey != null)
            {
                avalue = (string)regKey.GetValue(aKey);
                regKey.Close();
            }

            return avalue;
        }
        public void setRegValue(string aKey, string val)
        {
            //RegistryKey regKey = Registry.Currentuser.CreateSubKey(_RegistryKey);
            RegistryKey regKey = Registry.LocalMachine.CreateSubKey(_RegistryKey);
            regKey.SetValue(aKey, val);
            regKey.Close();
        }

        public System.Globalization.CultureInfo getCulture()
        {
            return CultureInfo;
        }

        private AppSetting()
        {
            CultureInfo = new System.Globalization.CultureInfo("vi-VN");
            //System.Windows.Forms.MessageBox.Show(CultureInfo.NumberFormat.CurrencyDecimalDigits.ToString());
            // Đặt lại số lẻ sau dấu chấm thập phân là không có số lẻ
            // Đặt lại ký tự tiền tệ là không có
            CultureInfo.NumberFormat.CurrencyDecimalDigits = 0;
            CultureInfo.NumberFormat.CurrencySymbol = string.Empty;
            CultureInfo.NumberFormat.CurrencyGroupSeparator = ".";
            CultureInfo.NumberFormat.CurrencyDecimalSeparator = ",";
            CultureInfo.DateTimeFormat.ShortTimePattern = "";
            CultureInfo.DateTimeFormat.LongTimePattern = "";
            if (m_AppName == "")
                configFileName = Environment.CurrentDirectory + "\\" + AppDomain.CurrentDomain.FriendlyName + ".config";
            else
                configFileName = Environment.CurrentDirectory + "\\" + m_AppName + ".config";
            if (!System.IO.File.Exists(configFileName))
            {
                try
                {
                    //Get default file config and write one
                    string defaultConfig = getDefaultConfig();
                    WriteFile(configFileName, defaultConfig);
                }
                catch
                {
                    // no configuration file exists
                    // and can't write a default one
                    throw new ApplicationException("Can't create configuration file for application (" + Environment.CurrentDirectory + "\\" + AppDomain.CurrentDomain.FriendlyName);
                }
            }
            try
            {
                // Create a new XmlDocument object
                xml = new XmlDocument();

                // load the configuration file
                xml.Load(configFileName);

                // get the <appSettings> element
                appSettingsElement = xml.SelectSingleNode("//configuration/appSettings");

                // if there's no <appSettings> section, create one.
                if (appSettingsElement == null)
                {
                    appSettingsElement = xml.CreateElement("appSettings");
                    xml.DocumentElement.AppendChild(appSettingsElement);
                }

                //DATETIME_FORMAT = this.getConfigValue("DATETIME_FORMAT");

            }
            catch (Exception ex)
            {
                // handle Load errors
                throw new ApplicationException("Unable to load the configuration " + configFileName +
                    " file for this application.", ex);
            }
            //}
            //else
            //{
            //    // no configuration file exists
            //    // you might want to alter this method to  create one
            //    throw new ApplicationException("This application (" + Environment.CurrentDirectory + "\\" + AppDomain.CurrentDomain.FriendlyName + ".config" + " has no configuration file.");

            //}
        }

        public void setConfigValue(String key, String val)
        {
            // create a new <add> element
            newElement = xml.CreateElement("add");
            String s = getConfigValue(key);
            if (s == null)
            {
                // set its attributes
                newElement.SetAttribute("key", key);
                //val = Security.Encrypt(val, "khaihoanproducts","takeru");
                newElement.SetAttribute("value", val);

                // append it to the <appSettings> section
                appSettingsElement.AppendChild(newElement);
            }
            else
            {
                XmlElement appsetting = (XmlElement)(appSettingsElement.SelectSingleNode("add[@key='" + key + "']"));
                if (appsetting != null)
                {
                    //val = Security.Encrypt(val, "khaihoanproducts","takeru");
                    appsetting.SetAttribute("value", val);
                }
            }
            try
            {
                xml.Save(configFileName);
            }
            catch
            {
                Thread.Sleep(3000);
                xml.Save(configFileName);
            }
        }

        public string getConfigValue(string aKey)
        {
            XmlElement appsetting = (XmlElement)(appSettingsElement.SelectSingleNode("add[@key='" + aKey + "']"));
            if (appsetting != null)
            {
                //return Security.Decrypt(appsetting.GetAttribute("value"), "khaihoan", "hontre");
                return (appsetting.GetAttribute("value"));
            }
            else return null;//String.Empty ;
        }

        public void Remove(string aKey)
        {
            XmlElement el = _GetSettingsElement(aKey);
            if (el != null)
            {
                el.ParentNode.RemoveChild(el);
                //dirty = True;
            }
            xml.Save(configFileName);
        }

        private XmlElement _GetSettingsElement(string aKey)
        {
            return (XmlElement)(appSettingsElement.SelectSingleNode("add[@key='" + aKey + "']"));
        }
        public static AppSetting getSetting(string AppName)
        {
            m_AppName = AppName;

            if (aps == null)
            {
                aps = new AppSetting();
                return aps;
            }
            else return aps;
        }
        public static AppSetting getSetting()
        {
            if (aps == null)
            {
                aps = new AppSetting();
                return aps;
            }
            else return aps;
        }
        public bool ExistKey(string aKey)
        {
            try
            {
                XmlElement appsetting = (XmlElement)(appSettingsElement.SelectSingleNode("add[@key='" + aKey + "']"));
                if (appsetting == null)
                    return false;

                return true;
            }
            catch
            {
                return false;
            }
        }

        private string getDefaultConfig()
        {
            return "";
            //return SMS_Agent.Resource1.DefaultConfigString;
        }

        private void WriteFile(string fileName, string content)
        {
            System.IO.TextWriter tw = new System.IO.StreamWriter(fileName, false);

            // write a line of text to the file
            tw.WriteLine(content);

            // close the stream
            tw.Close();
        }
    }

    public class RegSetting
    {

        private static string _RegistryKey = @"Software\Victory\eTicket";

        public static string c_RegistryKey
        {
            get { return _RegistryKey; }
            set { _RegistryKey = value; }
        }
        // set property cho _RegistryKey truoc khi goi 02 ham duoi day
        public static string getRegValue(string aKey)
        {
            string avalue = null;
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey(_RegistryKey);
            if (regKey != null)
            {
                avalue = (string)regKey.GetValue(aKey);
                regKey.Close();
            }

            return avalue;
        }
        public static void setRegValue(string aKey, string val)
        {
            //RegistryKey regKey = Registry.Currentuser.CreateSubKey(_RegistryKey);
            RegistryKey regKey = Registry.LocalMachine.CreateSubKey(_RegistryKey);
            regKey.SetValue(aKey, val);
            regKey.Close();
        }
    }
}